/**
 * 
 */
package edu.arizona.bio5.onto.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.URI;
import java.net.URL;
import java.net.URLEncoder;

/**
 * Used to download all ontologies from a repository
 *
 * (c) Pavel Klinov
 *
 */
public class OntologyBulkDownloader {

	/*private static final String REPO_PREFIX = "http://rest.bioontology.org/bioportal/ontologies/download/";
	private static final String REPO_LIST_FILE = "file:test_data/onto/bioportal.list";
	private static final String LOCAL_FOLDER = "test_data/onto/bioportal/";*/
	private static final String REPO_PREFIX = "http://owl.cs.manchester.ac.uk/repository/download?ontology=";
	private static final String REPO_LIST_FILE = "file:test_data/onto/tones.list";
	private static final String LOCAL_FOLDER = "test_data/onto/tones/";
	
	
	public static void main(String... arg) throws Exception {
		
		BufferedReader reader = new BufferedReader(new InputStreamReader(URI.create(REPO_LIST_FILE).toURL().openStream()));
		String line = null;
		File folder = new File(LOCAL_FOLDER);
		
		if (!folder.exists()) {
			
			folder.mkdir();
		}
		
		while ((line = reader.readLine()) != null) {
			
			if (line.startsWith("#")) continue;
			//Downloading a file
			try {
				downloadFile(line);
			} catch (Exception e) {
				// Perhaps broken URL?
				e.printStackTrace();
				//Go on to the next file
			}
		}
	}


	private static void downloadFile(String id) throws Exception {
		
		URL url = URI.create(REPO_PREFIX + id).toURL();
		InputStream in = url.openStream();
	    byte[] buf = new byte[4 * 1024]; // 4K buffer
	    int bytesRead = 0;
	    int total = 0;
	    
	    System.out.println("Downloading " + url.toString());
	    
	    id = URLEncoder.encode(id, "UTF-8");
	    
	    File outFile = new File(LOCAL_FOLDER + id + ".owl");
	    OutputStream out = new FileOutputStream(outFile);
	    
	    while ((bytesRead = in.read(buf)) != -1) {
	    
	    	total += bytesRead;
	    	
	    	if (total > 11000000) {
	    		//Ignore too big files
	    		out.flush();
	    		out.close();
	    		outFile.delete();
	    		break;
	    	}
	    	
	    	out.write(buf, 0, bytesRead);
	    }
	    
	    out.flush();
	    out.close();
	}
}
